#include<stdio.h>
#include<malloc.h>

//==========================================================================================================================================
/*int siza(int *ar,int size){
 
 size=sizeof(ar)/sizeof(int);
 return size;   
}*/


//==========================================================================================================================================


int *append(int *ar,int size){
    int no,i;
    int *p;
    p=(int *) malloc((size+1) * (sizeof(int)));
    for(i=0;i<size;i++){
                        p[i]=ar[i];
                        } 
    printf("enter the no. to be appended:");
    scanf("%d",&no);
    p[size]=no;
    printf("the new list is:");
    for(i=0;i<size+1;i++){
                          printf("%d ",p[i]);
                          }
                          
    return p;
}  



//========================================================================================================================================



int *extend(int *first,int *second,int firstsize,int secondsize){
	int *new;
	int i,newsize,j,p;
    newsize=firstsize + secondsize;
	new=(int *) malloc((newsize) * (sizeof(int)));
	for(i=0;i<firstsize;i++){
		new[i]=first[i];
			   }
	for(j=(firstsize),p=0;j<newsize,p<secondsize;j++,p++){
		new[j]=second[p];
			}
	printf("the new list is:");
	for(i=0;i<newsize;i++){
		printf("%d ",new[i]);
		}
return new;
}



//=====================================================================================================================================




int *delete(int *ar,int value,int size){
	int i,j;
    	int *p;
        p=(int *) malloc((size-1) * (sizeof(int)));	
	i=0;j=0;	
	while((i<size) && j<(size-1)){
		if(ar[i]!=value){
			p[j]=ar[i];
			i=i+1;
			j=j+1;
			}
		else{
			i=i+1;
			}
		}
	printf("the deleted list is:");
	for(i=0;i<(size-1);i++){
		printf("%d ",p[i]);
		}
					
return p;
}

//========================================================================================================================================

int *pop(int *ar,int in,int size){
	int i,j,ta;
    	int *p;
        p=(int *) malloc((size-1) * (sizeof(int)));	
	i=0;j=0;	
	while((i<size) && j<(size-1)){
		if(i!=in){
			p[j]=ar[i];
			i=i+1;
			j=j+1;
			}
		else{   
			ta=ar[i];                             
			i=i+1;
			}
		}
	printf("the poped list is:");
	for(i=0;i<(size-1);i++){
		printf("%d ",p[i]);
		}
	printf("\n");
        printf("The poped element is:%d \n",ta);
					
return p;
}




//===========================================================================================================================================




int index1(int *ar,int value,int size){
	int i,r;
	for(i=0;i<size;i++){
		if(ar[i]==value){
			r=i;
			break;
			break;	}
		else{
			r=999999;
			}
		}
	printf("the index is: %d\n",r);
	return 0;
}



//===========================================================================================================================================




int *insert(int *ar,int in,int value,int size){
	int *q;
        int i,j,flag;
	q=(int *) malloc( (size+1) * (sizeof(int)));
	i=0;
	j=0;
	flag=0;
    while((i<size) && (j<size+1)){
		if(j!=in){
			q[j]=ar[i];			
			i=i+1;
			j=j+1;
            }		
		else{
			q[j]=value;
			j=j+1;			
        }
        }
                       		
	printf("the new inserted list is:");
	for(i=0;i<size+1;i++){
		printf("%d ",q[i]);
		}
return q;
}



//=======================================================================================================================================



int sorti(int *ar,int size){
	int i;
	int j;
	int temp;
	for(i=0;i<size;i++){
		for(j=0;j<(size-i-1);j++){
			if(ar[j]>ar[j+1]){
				temp=ar[j+1];
				ar[j+1]=ar[j];
				ar[j]=temp;
				}}}
	printf("the sorted list is:");
	for(i=0;i<size;i++){
		printf("%d ",ar[i]);
			}
	return 0;
}


//===========================================================================================================================================




int *reversa(int *ar,int size){
    int i,j;
    int *p;
    p=(int *) malloc(size * (sizeof(int)));
    for(i=0,j=size-1;i<size,j>=0;i++,j--){
                                       p[i]=ar[j];
                                       }
    printf("The reversed list is:");
    for(i=0;i<size;i++){
                        printf("%d ",p[i]);
                        }
    return p;
}
//==============================================================================================================================================

int count(int *ar,int v,int size){
	int i;
	int c;
	c=0;
	for(i=0;i<size;i++){
		if(ar[i]==v){
			c=c+1;
			}
			}
	printf("the count of this no is: %d",c);
return 0;
}

//===============================================================================================================================================
int main(){
int i,s,option,p,n,pos;
printf("Enter the size of the array:");
scanf("%d",&s);
int *a,*b;
a=(int *) malloc( s * (sizeof(int)));
printf("Input the array \n");
for(i=0;i<s;i++){
	scanf("%d",&a[i]);
	}

while(1){
	printf("Enter your option\n 0.exit \n 1.append\n 2.extend \n 3.insert\n 4.delete \n 5.pop\n 6.index \n 7.sort \n 8.reverse \n 9.count\t \n");
	scanf(" %d",&option);
	switch (option)
	{
	case 1:{a=append(a,s);
		s=s+1;
		break;
		}
	case 2:{printf("enter the size of the array  you want to append at the end of the list:");
	       scanf("%d",&p);
	       b=(int *)(malloc(p*sizeof(int)));
	       for (i=0; i<p ;i++)
		   {
		   printf("enter the %d element of this array ", i+1);
		   scanf("%d", &b[i]);
		   }
	        a=extend(a,b,s,p);
	        s=s+p;
	        break;
                }
	case 3: {printf("enter the no you want to insert:");
		   scanf("%d",&n);
		   printf("enter the position at which you want to insert");
		   scanf("%d",&pos);
		   a=insert(a,pos,n,s);
		   s=s+1;
		   break;
                    }
	case 4: {printf("enter the no to be deleted:");
		scanf("%d",&n);
		a=delete(a,n,s);
		s=s-1;
		break;
                  }
	case 5: {printf("enter the position to be poped");
		scanf("%d",&pos);
		a=pop(a,pos,s);
		s=s-1;
		break;
                   }
	case 6 : {printf("enter the value");
		scanf("%d",&n);
		index1(a,n,s);
		break;
                  }
	case 7: {sorti(a,s);
		break;
                      }
	case 8: {reversa(a,s);
		break;
		}
	case 9: {printf("enter the value to be counted:");
		scanf("%d",&n);
		count(a,n,s);
		break;
		}
	case 0: {
		exit(1);
		break;
		}
                  

	}
	}
	return 0;
	}
	


























/*=============================================88888888888888888888888888888888888888888888888888888=========================================



int main(){
int *a;
int *g;
a=(int *) malloc(6 * (sizeof(int)));
g=(int *) malloc(3 * (sizeof(int)));
a[0]=21;a[1]=2;a[2]=450;a[3]=100;a[4]=237;a[5]=987;
g[0]=101;g[1]=102;g[2]=103;

//append(a,5);
//extend(a,g,4,3);
//delete(a,100,5);
//index1(a,23);
//insert(a,1,234,4);
//sorti(a,4);
//reversa(a,5);
//pop(a,3,6); 
return 0;
}

*/		

